import { ENUM_ERROR } from "~/error";
import { INTEGER_PG } from "~/db.pg";
import { RestDto } from "~/rest";

import { User } from "@/stream-user/schema.user";
import { Access } from "@/stream-access/schema.access";
import { AccessGateway } from "@/stream-access/dto.access";
import { AccessAll, AccessDroitContext } from "@/stream-access/core.define";

/** 某个访客，在指定集合上，是否具有授权（否则会报错）*/
export type checkDroitContextRes = { context: AccessDroitContext };

export const AccessRpcSearchPath = `${AccessGateway.path}/search`;
export type searchAccessRpcDto = { someoneId: User["uuid32"]; scopePathPart: string };

export const AccessRpcPutSyncPath = `${AccessGateway.path}/putSync`;
export type putAccessRpcDto = { refVisitorId: User["uuid32"]; scopeStr: string };

export const AccessRpcCheckStockNodeDroitContextPath = `${AccessGateway.path}/checkStockNodeDroitContext`;
export type checkStockNodeDroitContextRpcDto = { code: ENUM_ERROR };

export const AccessRpcCheckTonOrderGroupDroitContextPath = `${AccessGateway.path}/checkTonOrderGroupDroitContext`;
export type checkTonOrderGroupDroitContextRpcDto = { code: ENUM_ERROR; groupId: INTEGER_PG };

export const AccessRpcCheckVoucherGroupDroitContextPath = `${AccessGateway.path}/checkVoucherGroupDroitContext`;
export type checkVoucherGroupDroitContextRpcDto = { code: ENUM_ERROR; groupId: INTEGER_PG };

export interface AccessRpcInterface<T> {
  /** 返回和某人有关系的全部授权 */
  search(dto: T & searchAccessRpcDto): Promise<AccessAll>;

  /** 管理员创建一个新授权 */
  putSync(dto: T & putAccessRpcDto): Promise<Access>;

  /** 检查访客是否具有授权 */
  checkStockNodeDroitContext(dto: T & checkStockNodeDroitContextRpcDto): Promise<checkDroitContextRes>;

  /** 检查访客是否具有授权
   * @important 务必保证 groupId 和访客的管理人一致
   */
  checkTonOrderGroupDroitContext(dto: T & checkTonOrderGroupDroitContextRpcDto): Promise<checkDroitContextRes>;

  /** 检查访客是否具有授权
   * @important 务必保证 groupId 和访客的管理人一致
   */
  checkVoucherGroupDroitContext(dto: T & checkVoucherGroupDroitContextRpcDto): Promise<checkDroitContextRes>;
}
